package com.androidhive.androidlistview;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class SessionManager {
	// Shared Preferences
	SharedPreferences pref;
	
	// Editor for Shared preferences
	Editor editor;
	
	// Context
	Context _context;
	
	// Shared pref mode
	int PRIVATE_MODE = 0;
	
	// Sharedpref file name
	private static final String PREF_NAME = "AndroidHivePref";
	
	// All Shared Preferences Keys
	private static final String IS_LOGIN = "IsLoggedIn";
	
	// User name (make variable public to access from outside)
	public static final String IsFarmer = "No";
	public static final String Vendor_Id = "Id";
	
	// Email address (make variable public to access from outside)
	public static final String Crop_Id = "CropId";
	
	// Constructorp
	public SessionManager(Context context){
		this._context = context;
		pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
		editor = pref.edit();
	}
	
	/**
	 * Create login session
	 * */
	public void createLoginSessionVendorId(String Id){
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);
		
		// Storing name in pref
		editor.putString(Vendor_Id, Id);
		
		// Storing email in pref
		//editor.putString(Crop_Id, cropId);
		
		// commit changes
		editor.commit();
	}	
	public void setIsFarmer(String s){
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);
		
		// Storing name in pref
		//editor.putString(IsFarmer,s);
		editor.putString(IsFarmer, s);
		Log.d("IsFarmer:afterLogin",IsFarmer);
		
		// Storing email in pref
		//editor.putString(Crop_Id, cropId);
		
		// commit changes
		editor.commit();
	}	
	public void clearIsFarmer(){
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);
		
		// Storing name in pref
		editor.putString(IsFarmer,"No");
		
		// Storing email in pref
		//editor.putString(Crop_Id, cropId);
		
		// commit changes
		editor.commit();
	}	
	public HashMap<String, String> getIsFarmer(){
		HashMap<String, String> user = new HashMap<String, String>();
		// user name
		user.put(IsFarmer, pref.getString(IsFarmer, null));
		
		// user email id
		user.put(Crop_Id, pref.getString(Crop_Id, null));
		
		// return user
		return user;
	}
	public void createLoginSessionCropId(String cropId){
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);
		
		// Storing name in pref
		//editor.putString(Vendor_Id, Id);
		
		// Storing email in pref
		editor.putString(Crop_Id, cropId);
		
		// commit changes
		editor.commit();
	}	
	
	/**
	 * Check login method wil check user login status
	 * If false it will redirect user to login page
	 * Else won't do anything
	 * */
	
	
	
	
	/**
	 * Get stored session data
	 * */
	
	public HashMap<String, String> getUserDetails(){
		HashMap<String, String> user = new HashMap<String, String>();
		// user name
		user.put(Vendor_Id, pref.getString(Vendor_Id, null));
		
		// user email id
		user.put(Crop_Id, pref.getString(Crop_Id, null));
		
		// return user
		return user;
	}
	
	public HashMap<String, String> getVendorId(){
		HashMap<String, String> user = new HashMap<String, String>();
		// user name
		user.put(Vendor_Id, pref.getString(Vendor_Id, null));
		
		// user email id
		//user.put(Crop_Id, pref.getString(Crop_Id, null));
		user.put(Crop_Id, "$");
		
		// return user
		return user;
	}
	public HashMap<String, String> getCropId(){
		HashMap<String, String> user = new HashMap<String, String>();
		// user name
		user.put(Vendor_Id, "0");
		
		// user email id
		user.put(Crop_Id, pref.getString(Crop_Id, null));
		//user.put(Crop_Id, "$");
		
		// return user
		return user;
	}
	
	/**
	 * Clear session details
	 * */
	public void logoutUser(){
		// Clearing all data from Shared Preferences
		editor.clear();
		editor.commit();
		
	
	}
	
	/**
	 * Quick check for login
	 * **/
	// Get Login State
	public boolean isLoggedIn(){
		return pref.getBoolean(IS_LOGIN, false);
	}
}
